package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.QueryPageDTO;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.common.exception.BusinessException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Yan
 * @date 2022/2/25
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    @Override
    public Result<String> saveCategory(Category category) {
        if (ObjectUtils.isEmpty(category)) {
            return Result.error("新增失败");
        }

        if (ObjectUtils.isEmpty(category.getName())
                || ObjectUtils.isEmpty(category.getType())
                || ObjectUtils.isEmpty(category.getSort())) {
            return Result.error("新增失败");
        }

        this.save(category);

        return Result.success("新增分类成功");
    }

    /**
     * 分页查询
     *
     * @param queryPageDTO
     * @return
     */
    @Override
    public Result<Page<Category>> findByPage(QueryPageDTO queryPageDTO) {

        if (ObjectUtils.isEmpty(queryPageDTO)) {
            return Result.error("查询失败");
        }
        Long page = queryPageDTO.getPage();
        Long pageSize = queryPageDTO.getPageSize();

        if (ObjectUtils.isEmpty(page) || page <= 0) {
            page = 1L;
        }
        if (ObjectUtils.isEmpty(pageSize)||pageSize<=0){
            pageSize=10L;
        }

        Page<Category> categoryPage = new Page<>(page,pageSize);

        //排序
        LambdaQueryWrapper<Category> wrapper =new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        //分页查询
        this.page(categoryPage,wrapper);

        return Result.success(categoryPage);
    }

    /**
     * 删除分类的方法
     * @param id
     * @return
     */
    @Override
    public Result<String> deleteCategory(Long id) {
        if (ObjectUtils.isEmpty(id)){
            return Result.error("数据错误");
        }

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper =new LambdaQueryWrapper<>();

        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1>0){
            throw new BusinessException("当前分类下关联了菜品，不能删除");
            //return Result.error("删除的分类有关联");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper =new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new BusinessException("当前分类关联了套餐，不能删除");
        }

        boolean b = this.removeById(id);
        if (!b){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public Result<String> updateCategory(Category category) {
        if (ObjectUtils.isEmpty(category)){
            return Result.error("数据不对");
        }
        String name = category.getName();
        Integer sort = category.getSort();

        if (ObjectUtils.isEmpty(name)||ObjectUtils.isEmpty(sort)){
            return Result.error("数据不对");
        }

        this.updateById(category);

        return Result.success("修改成功");
    }

    @Override
    public Result<List<Category>> listCategory(Integer type) {

        /*if (ObjectUtils.isEmpty(type)){
            return Result.error("数据错误");
        }*/

        LambdaQueryWrapper<Category> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(type),Category::getType,type);
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = this.list(wrapper);

        return Result.success(list);
    }
}
